<?php
function sprintxml($tabs, $text){
	return str_repeat("\t",$tabs).$text."\n";
}


function _tablexml($tab, $table, $id, $rel_int=Array(), $rel_ext=Array()){
	// database
	global $bpc_db;
	$sql = "SELECT * FROM {$table[0]} WHERE {$table[1]}=$id";
	$xml= '';
	if ( $result = $bpc_db->query($sql) ){
		while ( $row = $result->fetch_assoc() ){		
			$xml .= sprintxml($tab, "<{$table[0]}>");
			foreach ($row as $key=>$value){
				if ( array_key_exists($key, $rel_int) ){
					$xml .= _tablexml($tab+1, $rel_int[$key], $row[$key]);
				}
				else {
					$xml .= sprintxml($tab+1,sprintf('<%1$s name="%3$s">%2$s</%1$s>', $key, $value, lng($key)));
				}
			}
			foreach ($rel_ext as $relation){	
				$xml .= sprintxml($tab+1, "<{$relation[0]}-ext>");
				$xml .= _tablexml($tab+2, $relation, $id);
				$xml .= sprintxml($tab+1, "</{$relation[0]}-ext>");
			}
			$xml .= sprintxml($tab, "</{$table[0]}>");
		}
	}
	else{
		return FALSE;
	}
	return $xml;
}


function tablexml($xsl, $table, $id, $rel_int=Array(), $rel_ext=Array()){
	
	$xsl = 'bpc_layout/'.$xsl;
	$xml = _tablexml(0, $table, $id, $rel_int, $rel_ext);
	if ($xml){
		header ("content-type: text/xml");
		$header = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
		$header .= "<?xml-stylesheet type='text/xsl' href='$xsl'?>\n";
		echo $header.$xml;
	}
	else {
		return FALSE;
	}
}
?>
